<template>
	<view class="pages-home">
		<!-- #ifndef H5 -->
		<uni-nav-bar :fixed="true" :shadow="false" :statusBar="true" title="首页"
			color="#ffffff" :backgroundColor="primaryColor">
		</uni-nav-bar>
		<view :style="{height:`${configInfo.navBarHeight}px`}"></view>
		<!-- #endif -->
		<view class="box-unaudited">
			<view class="f-mini-title text-center pb-lg c-disable">开工后即可接单哦~</view>
			<view class="box-unaudited-btn flex-center flex-column mt-md" @tap="clockIn"
			:style="{background: primaryColor}">
				<view class="c-base f-sm-title">开工打卡</view>
				<view class="f-mini-title c-base" style="opacity: 0.5;">{{currentDate}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import siteInfo from '@/siteinfo.js';
	import tabbar from "@/components/tabbar.vue"
	let timer = null
	export default {
		components: {
			tabbar
		},
		data() {
			return {
				currentDate: ''
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			coachInfo: state => state.user.coachInfo,
			isAppLogin: state => state.user.isAppLogin,
			autograph: state => state.user.autograph,
			userInfo: state => state.user.userInfo,
		}),
		async onLoad(options) {
			this.$util.showLoading()
			if(!this.autograph && uni.getStorageSync('autograph')){
				await this.updateUserItem({
					key: 'autograph',
					val: uni.getStorageSync('autograph')
				})
			}
			this.initIndex()
		},
		onUnload() {
			if(timer){
				clearInterval(timer)
			}
		},
		methods: {
			...mapActions(['getConfigInfo', 'getCoachInfo', 'getUserInfo']),
			...mapMutations(['updateHomeItem', 'updateUserItem']),
			async initIndex(refresh = false){
				this.currentDate = this.$util.formatTime('', 'h:m:s')
				timer = setInterval(()=>{
					this.currentDate = this.$util.formatTime('', 'h:m:s')
				},1000)
				
				if(!this.configInfo.id || !this.primaryColor || refresh){
					await this.getConfigInfo({is_coach: 0})
				}
				this.$util.hideAll()
			},
			async clockIn(){
				// this.getCoachInfo()
				if(this.autograph){
					this.$util.showLoading()
					await this.getUserInfo()
					this.$util.hideAll()
					let {
						coach_status = 0,
						company_status = 0
					} = this.userInfo
					if([2, 3].includes(coach_status) || [2, 3].includes(company_status)){
						this.$util.goUrl({url: '/pages/home', openType: 'reLaunch'})
						return
					}
					if([1, 4].includes(coach_status)){
						this.$util.goUrl({url: '/user/pages/service-apply-new', openType: 'navigateTo'})
						return
					}
					if([1, 4].includes(company_status)){
						this.$util.goUrl({url: '/user/pages/company-apply', openType: 'navigateTo'})
						return
					}
					if(coach_status == 0 || company_status == 0){
						this.$util.goUrl({url: '/login/pages/index', openType: 'reLaunch'})
					}
				}else{
					this.$util.goUrl({url: '/login/pages/index', openType: 'reLaunch'})
				}
			}
		},
	}
</script>


<style lang="scss">
	.pages-home {
		.box-unaudited{
			padding-top: 200rpx;
		}
		.box-unaudited-btn{
			width: 208rpx;
			height: 208rpx;
			border-radius: 208rpx;
			margin: 0 auto;
		}
	}
</style>
